﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MediaAdmin.Controllers
{
    [Authorize]
    public partial class PresetController : Controller
    {

        public ActionResult Index()
        {
            var di = new DirectoryInfo(HttpContext.Server.MapPath("~/App_Data/presets/"));
            var model = di.GetFiles().Select(x => new PresetSummary() { Name = x.Name });
            return View(model);
        }

        public ActionResult Delete(string id)
        {
            return View(new PresetSummary() { Name = id });
        }

        [HttpPost]
        public ActionResult Delete(PresetSummary model)
        {
            System.IO.File.Delete(HttpContext.Server.MapPath("~/App_Data/presets/"+model.Name));
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            var content = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/App_Data/presets/" + id));
            return View(new EditPresetModel() { Name = id, Content = content });
        }

        [HttpPost]
        public ActionResult Edit(EditPresetModel model)
        {
            System.IO.File.WriteAllText(HttpContext.Server.MapPath("~/App_Data/presets/" + model.Name), model.Content);
            return RedirectToAction("Index");
        }

        public ActionResult Create()
        {
            return View(new PresetSummary());
        }

        [HttpPost]
        public ActionResult UploadPreset(string name)
        {
            Request.Files[0].SaveAs(Server.MapPath("~/App_Data/presets/" + name));
            return Content("uploaded", "text/plain");
        }

    }
}
